package com.bringway.global.dao;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

/*
* @author qjy
* @version 创建时间：2015年9月9日 下午2:44:40
* 类说明
*/
public class P2PJdbcDao extends BaseJdbcDao{
	
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
	@Autowired
	public void setDataSource(@Qualifier("p2pDataSource") DataSource dataSource) {
	    this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
	}
	
	public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
		return namedParameterJdbcTemplate;
	}
	public int queryForInt(String tableName) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select count(*) from "+tableName+" ");
		Map<String, Object> parmsMap  =new HashMap<String,Object>();
		return this.getNamedParameterJdbcTemplate().queryForObject(sql.toString(), parmsMap, Integer.class);
	}
	
	public String getUUId(){
		StringBuffer sql = new StringBuffer();
		Map paramMap = new HashMap<>();
		sql.append(" SELECT UUID() FROM DUAL ");
		try{
			return this.getNamedParameterJdbcTemplate().queryForObject(sql.toString(), paramMap, String.class);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	public List<Map<String, Object>> local_queryForList(String sql, Map<String, Object> paramMap){
		return this.getNamedParameterJdbcTemplate().query(sql.toString(), paramMap, new ResultSetExtractor<List<Map<String, Object>>>() {

			@Override
			public List<Map<String, Object>> extractData(ResultSet rs) throws SQLException, DataAccessException {
				List<Map<String, Object>> list = new ArrayList<>();
				ResultSetMetaData rmeta = rs.getMetaData();
				int numColumns = rmeta.getColumnCount();
				while (rs.next()) {
					Map<String, Object> map = new HashMap<>();
					for (int i = 0; i < numColumns; i++) {
						map.put(rmeta.getColumnName(i + 1), rs.getString(i + 1));
					}
					list.add(map);
				}
				return list;
			}
		});
	}
	
	public int local_queryForInt(String sql, Map<String, Object> paramMap){
		return this.getNamedParameterJdbcTemplate().queryForObject(sql.toString(), paramMap, Integer.class);
	}
	
	public Map<String, String> local_queryForMap(String sql, Map<String, Object> paramMap){
		return this.getNamedParameterJdbcTemplate().query(sql.toString(), paramMap, new ResultSetExtractor<Map<String, String>>() {

			@Override
			public Map<String, String> extractData(ResultSet rs) throws SQLException, DataAccessException {
				ResultSetMetaData rmeta = rs.getMetaData();
				int numColumns = rmeta.getColumnCount();
				if (rs.next()) {
					Map<String, String> map = new HashMap<>();
					for (int i = 0; i < numColumns; i++) {
						map.put(rmeta.getColumnName(i + 1), rs.getString(i + 1));
					}
					return map;
				}
				return null;
			}
		});
	}
}
